/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.drugreturn.client.widget;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.carefx.framework.container.client.context.AppContext;

import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.framework.web.gwt.client.widget.FcMessageBox;
import com.th.supcom.pds.common.client.commom.PdsAuditPrescServiceFactory;
import com.th.supcom.pds.common.client.commom.PdsAuditPrescServiceFactory2;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.dispensedrug.client.service.PdsSupplyDetailRpcAsync;
import com.th.supcom.pds.dispensedrug.client.service.PdsSupplyRpcAsync;
import com.th.supcom.pds.drugreturn.client.service.RefundApplicationRpcAsync;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.internal.client.model.PrescAndPatResultModel;
import com.th.supcom.pds.prescstatusquery.client.widget.SupplyStatusQueryWindow;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * 门诊退药Action
 * 
 * @author yzhang
 */
public class RefundApplicationAction
{
    private static final PdsSupplyDetailRpcAsync PD_ASYNC = PdsAuditPrescServiceFactory.getPdsPrescDetailService ();
    private static final RefundApplicationRpcAsync S_REFUND_ASYNC = PdsAuditPrescServiceFactory2.getRefundApplicationService ();
    private static final PdsSupplyRpcAsync S_PRESC_RPC_ASYNC = PdsAuditPrescServiceFactory.getPdsPrescService ();
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private DrugReturnAndRefundFields m_drugReturnAndRefundFields;

    private DrugReturnAndRefundPrescPanel m_drugReturnAndRefundPrescPanel;

    private RefundApplicationPrescDetailPanel m_refundApplicationPrescDetailPanel;

    /** 用来存放处方和处方明细的内存空间 */
    private List <List <PdsPrescDetailInfoModel>> m_memoryList;

    /**
     * 构造函数
     */
    public RefundApplicationAction ()
    {

    }

    /**
     * 查询患者信息
     */
    public void initPatiInfo (PatientInfoModel paitentInfo)
    {
        mask ();
        m_drugReturnAndRefundFields.getPatiNameField ().setValue (paitentInfo.getName ());
        m_drugReturnAndRefundFields.getPatiIdField ().setValue (paitentInfo.getPatiId ());
        m_drugReturnAndRefundFields.getSexField ().setValue (paitentInfo.getSex ());
        DateTimeFormat dtFormat = DateTimeFormat.getFormat ("yyyy-MM-dd");
        if (paitentInfo.getBirthDate () != null)
        {
            m_drugReturnAndRefundFields.getBirthDaTetField ()
                                       .setValue (dtFormat.format (paitentInfo.getBirthDate ()));
        }
        // 查询余额
        m_drugReturnAndRefundFields.getRemainingSumField ().setValue (paitentInfo.getBalance ());
        if (paitentInfo.getChargeType () != null)
        {
            m_drugReturnAndRefundFields.getVisitTypeNameField ().setValue (paitentInfo.getChargeType ());
        }
        if (paitentInfo.getVisitId () != null)
        {
            m_drugReturnAndRefundFields.getVisitIdField ().setValue (paitentInfo.getVisitId ().toString ());
        }

       /* if (paitentInfo.getUpdateTime () != null)
        {
            m_drugReturnAndRefundFields.getVisitBeginDateField ()
                                       .setValue (dtFormat.format (paitentInfo.getMedicareCardPatientInfo ()
                                                                              .getUpdateTime ()));
        }*/
        m_drugReturnAndRefundFields.getDeptNameField ().setValue (paitentInfo.getDeptName ());
        m_drugReturnAndRefundFields.getDay ().setValue (paitentInfo.getVisitDay ()+"");
        m_drugReturnAndRefundFields.getNumber ().setValue (paitentInfo.getBedCode ());
    }

    public void queryByPid (final String patientId, Date enterDate)
    {
        /*S_PRESC_RPC_ASYNC.findPrescNoListByPatientId (patientId, enterDate, new AsyncCallback <List <String>> ()
        {

            @Override
            public void onFailure (Throwable caught)
            {
                unmask ();
                return;

            }

            @Override
            public void onSuccess (List <String> result)
            {
                String performedBy = ClientUtils.getOrgModel().getOrgCode();
                S_REFUND_ASYNC.findPrescDetailByPresc (result, performedBy,
                                                       new AsyncCallback <PrescAndPatResultModel> ()
                                                       {

                                                           @Override
                                                           public void onSuccess (PrescAndPatResultModel result)
                                                           {
                                                               if (result == null
                                                                   || result.getPdsPrescDetailInfoModellist ()
                                                                            .isEmpty ())
                                                               {
                                                                   ClientUtils.alertMessageBox ("找不到相关处方。",
                                                                                                m_drugReturnAndRefundFields.getMedicareCardField ());

                                                                   unmask ();
                                                                   clear (false);
                                                                   return;
                                                               }
                                                               m_drugReturnAndRefundPrescPanel.getStore ().removeAll ();
                                                               m_refundApplicationPrescDetailPanel.getPrescDetailGrid ()
                                                                                                  .getGrid ()
                                                                                                  .getStore ()
                                                                                                  .removeAll ();
                                                               m_memoryList = result.getPdsPrescDetailInfoModellist ();
                                                               m_drugReturnAndRefundPrescPanel.getStore ()
                                                                                              .add (result.getPdsPrescDetailInfoModellist ()
                                                                                                          .get (0));
                                                               m_drugReturnAndRefundFields.getPrescSource ()
                                                                                          .setValue (m_drugReturnAndRefundPrescPanel.getStore ()
                                                                                                                                    .getAt (0)
                                                                                                                                    .getPrescSourceName ());
                                                               unmask ();

                                                           }

                                                           @Override
                                                           public void onFailure (Throwable caught)
                                                           {
                                                               clear (false);
                                                               ClientUtils.alertMessageBox (caught.getMessage (),
                                                                                            m_drugReturnAndRefundFields.getMedicareCardField ());
                                                               unmask ();
                                                           }
                                                       });
            }
        });*/
    }

    /**
     * 清除
     */
    public void clear (final boolean isClearAll)
    {
        if (isClearAll)
        {
            m_drugReturnAndRefundFields.getPrescNoField ().clear ();
            m_drugReturnAndRefundFields.getMedicareCardField ().clear ();
        }
        m_memoryList = null;
        m_refundApplicationPrescDetailPanel.getPrescDetailGrid ().refresh (null);
        m_drugReturnAndRefundPrescPanel.refresh (null);
        m_drugReturnAndRefundFields.getPrescSource ().clear ();
        m_drugReturnAndRefundFields.getPatiNameField ().clear ();
        m_drugReturnAndRefundFields.getPatiIdField ().clear ();
        m_drugReturnAndRefundFields.getBirthDaTetField ().clear ();
        m_drugReturnAndRefundFields.getRemainingSumField ().clear ();
        m_drugReturnAndRefundFields.getSexField ().clear ();

        m_drugReturnAndRefundFields.getVisitTypeNameField ().clear ();
        m_drugReturnAndRefundFields.getVisitIdField ().clear ();
        m_drugReturnAndRefundFields.getVisitBeginDateField ().clear ();
        m_drugReturnAndRefundFields.getDeptNameField ().clear ();
        m_drugReturnAndRefundFields.getDay ().clear ();
        m_drugReturnAndRefundFields.getNumber ().clear ();

    }

    /**
     * 查询处方和处方明细信息
     * 
     * @param successComponent
     * @param failComponent
     */
    private void findPrescDetail (final Component successComponent, final Component failComponent)
    {
        final String prescNo = m_drugReturnAndRefundFields.getPrescNoField ().getValue ();
        final PdsPrescDetailInfoModel model = new PdsPrescDetailInfoModel ();
        model.setPrescNo (prescNo);
        model.setPerformedBy (ClientUtils.getOrgModel().getOrgCode()); 

        RefundApplicationAction.PD_ASYNC.getPrescDetailByPrescAndRcpt (model.getRcptNo (),
                                                                       model.getPrescNo (),
                                                                       new AsyncCallback <List <PdsPrescDetailInfoModel>> ()
                                                                       {
                                                                           @Override
                                                                           public void onFailure (final Throwable caught)
                                                                           {
                                                                               MessageBox.alert ("警告",
                                                                                                 caught.getMessage (),
                                                                                                 null);
                                                                               clear (false);
                                                                               return;
                                                                           }

                                                                           @Override
                                                                           public void onSuccess (final List <PdsPrescDetailInfoModel> result)
                                                                           {
                                                                               if (null == result || result.isEmpty ())
                                                                               {
                                                                                   ClientUtils.alertMessageBox ("处方号不存在，请重新输入。",
                                                                                                                failComponent);
                                                                                   clear (false);
                                                                                   return;
                                                                               }
                                                                               List <String> prescNoList = new ArrayList <String> ();
                                                                               prescNoList.add (model.getPrescNo ());
                                                                               S_REFUND_ASYNC.findPrescDetailByPresc (prescNoList,
                                                                                                                      model.getPerformedBy (),
                                                                                                                      new AsyncCallback <PrescAndPatResultModel> ()
                                                                                                                      {

                                                                                                                          @Override
                                                                                                                          public void onFailure (Throwable caught)
                                                                                                                          {
                                                                                                                              clear (false);
                                                                                                                              ClientUtils.alertMessageBox (caught.getMessage (),
                                                                                                                                                           failComponent);
                                                                                                                          }

                                                                                                                          @Override
                                                                                                                          public void onSuccess (PrescAndPatResultModel result)
                                                                                                                          {
                                                                                                                              if (ClientUtils.isEmpty (result.getPdsPrescDetailInfoModellist ()))
                                                                                                                              {
                                                                                                                                  final PdsSupplyModel currentCondition = new PdsSupplyModel ();
                                                                                                                                  currentCondition.setPrescNo (m_drugReturnAndRefundFields.getPrescNoField ()
                                                                                                                                                                                          .getValue ());
                                                                                                                                  final SupplyStatusQueryWindow window = new SupplyStatusQueryWindow (
                                                                                                                                                                                                    currentCondition,
                                                                                                                                                                                                    m_drugReturnAndRefundFields.getPrescNoField ());
                                                                                                                                  window.show ();
                                                                                                                                  clear (false);
                                                                                                                                  unmask ();
                                                                                                                                  return;
                                                                                                                              }
                                                                                                                              clear (false);
                                                                                                                              m_memoryList = result.getPdsPrescDetailInfoModellist ();
                                                                                                                              m_drugReturnAndRefundPrescPanel.getStore ()
                                                                                                                                                             .add (result.getPdsPrescDetailInfoModellist ()
                                                                                                                                                                         .get (0));
                                                                                                                              initPatiInfo (result.getMedicareCardAllInfoModel ());
                                                                                                                              unmask ();
                                                                                                                              if (successComponent != null)
                                                                                                                              {
                                                                                                                                  successComponent.focus ();
                                                                                                                              }

                                                                                                                          }

                                                                                                                      });
                                                                           }
                                                                       });

    }

    /**
     * 当点击主处方表格处方时 处方明细表格相应弹出或隐藏内容
     * 
     * @param selectedModel
     */
    public void doSelectedChange (List <PdsPrescDetailInfoModel> selectedModel)
    {
        if (ClientUtils.isEmpty (selectedModel))
        {
            m_refundApplicationPrescDetailPanel.getPrescDetailGrid ().getGrid ().getStore ().removeAll ();
        }
        if (ClientUtils.isEmpty (m_memoryList))
        {
            return;
        }
        final List <PdsPrescDetailInfoModel> prescDetails = ClientUtils.getPrescDetailSelected (selectedModel,
                                                                                                m_memoryList.get (1));
        m_refundApplicationPrescDetailPanel.getPrescDetailGrid ().getGrid ().getStore ().removeAll ();
        m_refundApplicationPrescDetailPanel.getPrescDetailGrid ().getGrid ().getStore ().add (prescDetails);
    }

    /**
     * 通过处方号查询处方和处方明细信息
     * 
     * @param successComponent
     * @param failComponent
     */
    public void findPrescDetailByPresc (final Component successComponent, final Component failComponent)
    {
        mask ();
        clear (false);
        findPrescDetail (successComponent, failComponent);
        unmask ();
    }

    private void mask ()
    {
        m_drugReturnAndRefundPrescPanel.mask ("加载中...");
        m_refundApplicationPrescDetailPanel.mask ("加载中...");
    }

    /**
     * 门诊退药确认
     * 
     * @param successComponent
     * @param failComponent
     */
    public void refundApplicate (final Component successComponent, final Component failComponent)
    {
        if (m_drugReturnAndRefundPrescPanel.getCheckmodel ().getSelectedItems ().isEmpty ())
        {
            ClientUtils.alertMessageBox ("请先选中相应的处方信息再进行此操作。", failComponent);
            return;
        }
        FcMessageBox.confirm (CodeConstants.PROMPT, "确定进行退费申请操作?", new Listener <MessageBoxEvent> ()
        {
            @Override
            public void handleEvent (final MessageBoxEvent be)
            {
                final Button btn = be.getButtonClicked ();
                if (btn.getText ().equals ("否"))
                {
                    failComponent.focus ();
                    return;
                }

                // 获取勾选处方主记录
                final List <PdsPrescDetailInfoModel> prescLists = m_drugReturnAndRefundPrescPanel.getCheckmodel ()
                                                                                                 .getSelectedItems ();

                final List <PdsPrescDetailInfoModel> prescDetailList = m_refundApplicationPrescDetailPanel.getPrescDetailGrid ()
                                                                                                          .getDetailData ();
                final List <PdsPrescDetailInfoModel> prescDetailListSelected = ClientUtils.getPrescDetailSelected (prescLists,
                                                                                                                   prescDetailList);
                final String operator = ClientUtils.geteEmpModel().getEmpCode();
                S_REFUND_ASYNC.refundApplicate (prescLists, prescDetailListSelected, operator,
                                                new AsyncCallback <Void> ()
                                                {

                                                    @Override
                                                    public void onFailure (final Throwable caught)
                                                    {
                                                        ClientUtils.alertMessageBox (caught.getLocalizedMessage (),
                                                                                     failComponent);
                                                    }

                                                    @Override
                                                    public void onSuccess (final Void result)
                                                    {
                                                        Info.display (CodeConstants.PROMPT, "退费申请成功");
                                                        clear (true);
                                                    }
                                                });
            }
        }, Dialog.YES);
    }

    public void setDrugReturnAndRefundFields (final DrugReturnAndRefundFields drugReturnAndRefundFields)
    {
        m_drugReturnAndRefundFields = drugReturnAndRefundFields;
    }

    public void setDrugReturnAndRefundPrescPanel (final DrugReturnAndRefundPrescPanel drugReturnAndRefundPrescPanel)
    {
        m_drugReturnAndRefundPrescPanel = drugReturnAndRefundPrescPanel;
    }

    public void setRefundApplicationPrescDetailPanel (final RefundApplicationPrescDetailPanel refundApplicationPrescDetailPanel)
    {
        m_refundApplicationPrescDetailPanel = refundApplicationPrescDetailPanel;
    }

    private void unmask ()
    {
        m_drugReturnAndRefundPrescPanel.unmask ();
        m_refundApplicationPrescDetailPanel.unmask ();
    }
}
